var scene, camera, light, renderer, container;
var status,monitor;
var cameraParams = {};
var width, height, loadManager, objLoader;
function initEnv() {
  width = window.innerWidth;
  height = window.innerHeight;
  loadManager = new THREE.LoadingManager();
  objLoader = new THREE.OBJLoader(loadManager);

  container = document.getElementById("container");
  scene = new THREE.Scene();
  camera = new THREE.PerspectiveCamera(45, width / height, 1, 100);
  light = new THREE.AmbientLight(0xefef0f);
  renderer = new THREE.WebGLRenderer({ antialias: false });
  renderer.setSize(width, height);

  camera.position.x = 0;
  camera.position.y = 0;
  camera.position.z = 5;
  camera.lookAt({
    x:0,y:0,z:0
  })
  scene.add(light);
  console.log("scene",scene)
  container.appendChild(renderer.domElement);
}


function initStatus() {
  stats = new Stats();
  stats.domElement.style.position = "absolute";
  stats.domElement.style.top = "0px";
  container.appendChild(stats.domElement);
  container.appendChild(renderer.domElement);
}

function initMonitor() {

  cameraParams.up = {};
  cameraParams.x = 0;
  cameraParams.y = 0;
  cameraParams.z = 5;

  monitor = new DAT.GUI();
  monitor.add(cameraParams, "x", -2000, 2000).name("相机X的位置");
  monitor.add(cameraParams, "y", -2000, 2000).name("相机Y的位置");
  monitor.add(cameraParams, "z", -2000, 5000).name("相机Z的位置");
}

function init() {
  initEnv();
  initStatus();
  initMonitor();
  loadModel();
}

function modelLoadProcess(name) {
  return function () {
    console.log(`${name} load process...`, arguments);
  }
}

function modelLoadErr(name) {
  return function () {
    console.log(`${name} load err...`, arguments);
  }
}
function loadModel() {
  let objURL = "../model/cube.obj";
  // let objURL = "../model/male02.obj";
  objLoader.load(objURL, function (obj) {
    console.log("obj",obj)
    // obj.position.y -= 10
    // obj.position.x -= 80
    scene.add(obj);
  }, modelLoadProcess("test"), modelLoadErr("test"))
}

function updateMonitor(){
  camera.position.x = cameraParams.x;
  camera.position.y = cameraParams.y;
  camera.position.z = cameraParams.z;
  camera.lookAt( scene.position );
  camera.updateProjectionMatrix();
}
function animate() {
  stats.update();
  updateMonitor();
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

function main() {
  init();
  animate();
}